package com.ymm.lulutong.domain.impl;

import com.ymm.lulutong.constant.ResultCodeEnum;
import com.ymm.lulutong.domain.ShipperNoticeDomainService;
import com.ymm.lulutong.entity.Notice;
import com.ymm.lulutong.mapper.ShipperNoticeMapper;
import com.ymm.lulutong.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Objects;

@Service
@Slf4j
public class ShipperNoticeDomainServiceImpl implements ShipperNoticeDomainService {
    @Resource
    ShipperNoticeMapper shipperNoticeMapper;

    /**
     * 保存（投递）通知
     *
     * @param notice 通知实体
     * @return 是否成功
     */
    @Override
    public Result<Boolean> saveNotice(Notice notice) {
        if (Objects.isNull(notice)) {
            return Result.fail(false, ResultCodeEnum.PARAMETER_NULL_ERROR.getCode(), ResultCodeEnum.PARAMETER_NULL_ERROR.getMessage());
        }
        notice.setRead(false);
        notice.setCreateTime(new Date());
        notice.setUpdateTime(new Date());
        try {
            shipperNoticeMapper.add(notice);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("错误");
            return Result.fail(null, ResultCodeEnum.DATABASE_ERROR.getCode(), ResultCodeEnum.DATABASE_ERROR.getMessage());
        }
        return Result.success(true);
    }

    /**
     * 读取单个通知
     *
     * @param noticeId 通知Id
     * @return 通知实体
     */
    @Override
    public Result<Notice> readNotice(Long noticeId, Long shipperId) {
        if (Objects.isNull(noticeId)) {
            return Result.fail(null, ResultCodeEnum.PARAMETER_NULL_ERROR.getCode(), ResultCodeEnum.PARAMETER_NULL_ERROR.getMessage());
        }
        Notice notice;
        try {
            notice = shipperNoticeMapper.getByNoticeId(noticeId);
            if (Objects.isNull(notice)) {
                return Result.fail(null, ResultCodeEnum.FAIL.getCode(), "无此消息");
            }
            if (!notice.getShipperId().equals(shipperId)) {
                return Result.fail(null, ResultCodeEnum.FAIL.getCode(), "不是属于你的通知你为什么要读？");
            }
            //设为已读状态
            shipperNoticeMapper.setReadStatus(noticeId, true);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("错误");
            return Result.fail(null, ResultCodeEnum.DATABASE_ERROR.getCode(), ResultCodeEnum.DATABASE_ERROR.getMessage());
        }
        return Result.success(notice);
    }

    /**
     * 统计所有未读通知的数量
     *
     * @param shipperId 货主Id
     * @return 所有未读通知的数量
     */
    @Override
    public Result<Integer> countNoticeUnread(Long shipperId) {
        if (Objects.isNull(shipperId)) {
            return Result.fail(null);
        }
        int count;
        try {
            count = shipperNoticeMapper.countNoticeUnread(shipperId);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("错误");
            return Result.fail(null, ResultCodeEnum.DATABASE_ERROR.getCode(), ResultCodeEnum.DATABASE_ERROR.getMessage());
        }
        return Result.success(count);
    }

    /**
     * 根据通知类型统计未读通知的数量
     *
     * @param shipperId  货主Id
     * @param noticeType 通知类型
     * @return 根据通知类型统计未读通知的数量
     */
    @Override
    public Result<Integer> countNoticeUnreadByNoticeType(Long shipperId, Integer noticeType) {
        if (Objects.isNull(shipperId) || Objects.isNull(noticeType)) {
            return Result.fail(null, ResultCodeEnum.PARAMETER_NULL_ERROR.getCode(), ResultCodeEnum.PARAMETER_NULL_ERROR.getMessage());
        }
        int count;
        try {
            count = shipperNoticeMapper.countNoticeUnreadByNoticeType(shipperId, noticeType);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("错误");
            return Result.fail(null, ResultCodeEnum.DATABASE_ERROR.getCode(), ResultCodeEnum.DATABASE_ERROR.getMessage());
        }
        return Result.success(count);
    }

//    @Override
//    public Result<List<Notice>> batchReadNotices(List<Integer> noticeIds) {
//        if (Objects.isNull(noticeIds)) {
//            return Result.fail(null, ResultCodeEnum.PARAMETER_NULL_ERROR.getCode(), ResultCodeEnum.PARAMETER_NULL_ERROR.getMessage());
//        }
//        List<Notice> notices;
//        try {
//            notices = shipperNoticeMapper.listNoticesByNoticeIds(noticeIds);
//            shipperNoticeMapper.batchSetReadStatus(noticeIds, true);
//        } catch (Exception e) {
//            e.printStackTrace();
//            log.error("错误");
//            return Result.fail(null, ResultCodeEnum.DATABASE_ERROR.getCode(), ResultCodeEnum.DATABASE_ERROR.getMessage());
//        }
//        return Result.success(notices);
//    }

    @Override
    public Result<List<Notice>> listAllNotices(Long shipperId) {
        if (Objects.isNull(shipperId)) {
            return Result.fail(null, ResultCodeEnum.PARAMETER_NULL_ERROR.getCode(), ResultCodeEnum.PARAMETER_NULL_ERROR.getMessage());
        }
        List<Notice> notices;
        try {
            notices = shipperNoticeMapper.listNoticesByShipperId(shipperId);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("错误");
            return Result.fail(null, ResultCodeEnum.DATABASE_ERROR.getCode(), ResultCodeEnum.DATABASE_ERROR.getMessage());
        }
        return Result.success(notices);
    }

    @Override
    public Result<List<Notice>> listNoticesByNoticeType(Long shipperId, Integer noticeType) {
        if (Objects.isNull(shipperId) || Objects.isNull(noticeType)) {
            return Result.fail(null, ResultCodeEnum.PARAMETER_NULL_ERROR.getCode(), ResultCodeEnum.PARAMETER_NULL_ERROR.getMessage());
        }
        List<Notice> notices;
        try {
            notices = shipperNoticeMapper.listNoticesByShipperIdAndNoticeType(shipperId, noticeType);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("错误");
            return Result.fail(null, ResultCodeEnum.DATABASE_ERROR.getCode(), ResultCodeEnum.DATABASE_ERROR.getMessage());
        }
        return Result.success(notices);
    }
}
